//
//  OtherShopCell.swift
//  iModern
//
//  Created by 立雄 王 on 15/8/19.
//  Copyright (c) 2015年 com.starlooking. All rights reserved.
//

import UIKit

class OtherShopCell: TableViewCell
{

    var activity = NSDictionary()

    let __avatar_image_view_tags = 60
    let __name_label_tag         = 61
    let __commend_label_tag      = 62
    let __evaluate_label_tag     = 63
    let __price_label_tag        = 64
    let __buy_button_tag         = 65

    override func awakeFromNib()
    {
        super.awakeFromNib()
        // Initialization code

        initAvatarImageView()
        initNameLabel()
        initCommendLabel()
        initEvaluateLabel()
        initPriceLabel()
        initBuyButton()
    }

    func setData(data: NSMutableDictionary)
    {
        activity = data
        let advertising = data["article"] as! [String:AnyObject]
        //let contact     = advertising["contact"] as! NSDictionary

        setAvatarImageView(advertising["imageURL"] as? String)

        let commend      = advertising["numberOfFavorites"] as! Int
        let labelcommend = self.viewWithTag(__commend_label_tag) as! UILabel
        labelcommend.text = "❤️\(commend)人点赞"

        let price      = advertising["price"] as! Int
        let labelprice = self.viewWithTag(__price_label_tag) as! UILabel
        labelprice.text = "价格:\(price)元"

        let indexOfSaleTags = advertising["summary"] as? String
        let nameLabel       = self.viewWithTag(__name_label_tag) as! UILabel
        nameLabel.text = "商品名称:" + indexOfSaleTags!

        let button = self.viewWithTag(__buy_button_tag) as! UIButton
        button.addTarget(self, action: Selector("buy"), forControlEvents: UIControlEvents.TouchUpInside)

        let rateOfPraise  = advertising["rateOfPraise"] as! Int
        let evaluateLabel = self.viewWithTag(__evaluate_label_tag) as! UILabel
        evaluateLabel.text = "好评度: \(rateOfPraise * 100)%"
    }

    func setAvatarImageView(thumbnailURL: String?)
    {
        let imageView = self.viewWithTag(__avatar_image_view_tags) as! UIImageView
        if thumbnailURL != nil
        {
            let url = appDelegate.parameterManager.server + "/" + thumbnailURL!
            imageView.load(url, placeholder: nil)
        }
        else
        {
            imageView.image = UIImage(named: "Avatar-Default.png")
        }
    }

    func initAvatarImageView()
    {
        var imageView = self.viewWithTag(__avatar_image_view_tags) as? UIImageView
        if (imageView == nil)
        {
            let frame = CGRect(x: 10.0, y: 10.0, width: 90.0, height: 90.0)
            imageView = UIImageView(frame: frame)
            imageView!.image = UIImage(named: "Avatar-Default.png")
            imageView!.tag = __avatar_image_view_tags
            imageView!.layer.masksToBounds = true
            imageView!.layer.borderColor = UIColor.groupTableViewBackgroundColor().CGColor
            imageView!.layer.borderWidth = 1.0
            self.addSubview(imageView!)
        }
    }

    func initNameLabel()
    {
        var label  = self.viewWithTag(__name_label_tag) as? UILabel
        if (label == nil)
        {
            let frame = CGRect(x: 110, y: 5, width: screen.size.width - 110, height: 40)
            label = UILabel(frame: frame)
            label!.font = UIFont.systemFontOfSize(16)
            label!.tag = __name_label_tag
            label?.text = "商品名称:软陶-定制任何样式包材料"
            label?.textColor = UIColor.blackColor()
            self.addSubview(label!)
        }
    }

    func initCommendLabel()
    {
        var label = self.viewWithTag(__commend_label_tag) as? UILabel
        if (label == nil)
        {
            let frame = CGRect(x: 110, y: 30, width: 100, height: 40)
            label = UILabel(frame: frame)
            label!.font = UIFont.systemFontOfSize(12)
            label!.tag = __commend_label_tag
            label?.text = "❤️xx人点赞"
            label?.textColor = UIColor.grayColor()
            self.addSubview(label!)
        }
    }

    func initEvaluateLabel()
    {
        screen
        var label = self.viewWithTag(__evaluate_label_tag) as? UILabel
        if (label == nil)
        {
            let frame = CGRect(x: screen.width - 120, y: 30, width: 110, height: 40)
            label = UILabel(frame: frame)
            label!.font = UIFont.systemFontOfSize(12)
            label!.tag = __evaluate_label_tag
            label?.text = "好评度: 10"
            label?.textColor = UIColor.grayColor()
            self.addSubview(label!)
        }
    }

    func initPriceLabel()
    {
        var label = self.viewWithTag(__price_label_tag) as? UILabel
        if (label == nil)
        {
            let frame = CGRect(x: 110, y: 70, width: 110, height: 40)
            label = UILabel(frame: frame)
            label!.font = UIFont.systemFontOfSize(12)
            label!.tag = __price_label_tag
            label?.text = "价格: 10元"
            label?.textColor = UIColor.orangeColor()
            self.addSubview(label!)
        }
    }

    func initBuyButton()
    {
        var button = self.viewWithTag(__buy_button_tag) as? UIButton
        if button == nil
        {
            let frame = CGRect(x: screen.width - 140, y: 70, width: 120, height: 30)
            button = UIButton(frame: frame)
            button!.setTitle("立即下单", forState: .Normal)
            button!.titleLabel?.font = UIFont.systemFontOfSize(12)
            button!.setTitleColor(UIColor.whiteColor(), forState: .Normal)
            button!.backgroundColor = UIColor(red: 0.40784313725490196, green: 0.6588235294117647, blue: 0.19215686274509805, alpha: 1)
            button!.layer.masksToBounds = true
            button!.layer.cornerRadius = 4
            button!.tag = __buy_button_tag
            self.addSubview(button!)
        }
    }

    func buy()
    {
//        if appDelegate.isLogged()
//        {
//            let controller = MakeAppointmentFormViewController()
//            controller.advertising = activity
//            appDelegate.navigationController?.pushViewController(controller, animated: true)
//        }
//        else
//        {
//            let controller = LoginViewController()
//            appDelegate.navigationController?.pushViewController(controller, animated: true)
//        }
        let article = activity["article"] as! NSDictionary
        let status = article["status"] as? String
        if status == "Ready for Sale"
        {
            if appDelegate.isLogged()
            {
                let controller = MakeAppointmentFormViewController()
                controller.advertising = activity
                appDelegate.navigationController?.pushViewController(controller, animated: true)
            }
            else
            {
                self.login()
            }
        }
        else
        {
            self.alertWarningMessage("该商品已下架")
            return
        }

    }

    override func setSelected(selected: Bool, animated: Bool)
    {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
